'use strict';

const Controller = require('egg').Controller;


const mysql = require('mysql')

const jwt = require('jsonwebtoken')

class HomeController extends Controller {


  //渲染
  async getList() {
    const { ctx, app } = this;
    const data = await app.mysql.select('lists')
    ctx.body = {
      code: 200,
      data
    }
  }

  //登录
  async Login() {
    const { ctx, app } = this
    const { username, password } = ctx.request.body
    console.log(ctx.request.body);
    const date = await app.mysql.get('login', { username, password })
    const token = jwt.sign(username, 'xxx')

    if (date) {
      ctx.body = {
        code: 1,
        msg: '登录成功',
        token,
        date
      }
    } else {
      ctx.body = {
        code: 0,
        msg: '登录失败'
      }
    }
  }

  //注册
  async register() {
    const { ctx, app } = this
    const { username, password } = ctx.request.body
    console.log(ctx.request.body);
    const date = await app.mysql.get('login', { username })
    if (date) {
      ctx.body = {
        code: 2,
        msg: '账户已存在'
      }
    } else {
      await app.mysql.insert('login', { username, password })
      ctx.body = {
        code: 3,
        msg: '注册成功'
      }
    }
  }

  //详情
  async detail() {
    const { ctx, app } = this;
    const { id } = ctx.request.body
    console.log(id);
    const data = await app.mysql.get("lists", { id })
    ctx.body = {
      code: 200,
      msg: "成功",
      data,
    }
  }

  //区域筛选
  async screening1() {
    const { app, ctx } = this;
    const { value } = ctx.request.body;
    const data = await app.mysql.select("lists", { where: { city: value } })

    if (value === '区域') {
      ctx.body = {
        code: 1,
        data
      }
    } else {
      ctx.body = {
        code: 500,
        data
      }
    }
  }

  //样式筛选
  async screening3() {
    const { app, ctx } = this;
    const { value } = ctx.request.body;
    const data = await app.mysql.select("lists", { where: { type: value } })

    if (value === '户型') {
      ctx.body = {
        code: 1,
        data
      }
    } else {
      ctx.body = {
        code: 500,
        data
      }
    }
  }

  //方位筛选
  async screening4() {
    const { app, ctx } = this;
    const { value } = ctx.request.body;
    const data = await app.mysql.select("lists", { where: { desc: value } })

    if (value === '筛选') {
      ctx.body = {
        code: 1,
        data
      }
    } else {
      ctx.body = {
        code: 500,
        data
      }
    }
  }

  async screening5() {
    const { app, ctx } = this;
    const { value } = ctx.request.body;
    const data = await app.mysql.select("lists", { where: { qu: value } })

    if (value === '筛选') {
      ctx.body = {
        code: 1,
        data
      }
    } else {
      ctx.body = {
        code: 500,
        data
      }
    }
  }


  async screening6() {
    const { app, ctx } = this;
    const { value } = ctx.request.body;
    const data = await app.mysql.select("lists", { where: { fang: value } })

    if (value === '筛选') {
      ctx.body = {
        code: 1,
        data
      }
    } else {
      ctx.body = {
        code: 500,
        data
      }
    }
  }
  //搜索title
  async search() {
    const { ctx, app } = this
    const { val } = ctx.request.body
    const data = await app.mysql.query(`select * from lists where title like '%${val}%'`)
    ctx.body = {
      code: 200,
      data
    }
  }

  //搜索name
  async search1() {
    const { ctx, app } = this
    const { val } = ctx.request.body
    const data = await app.mysql.query(`select * from lists where name like '%${val}%'`)
    ctx.body = {
      code: 200,
      data
    }
  }

  //删除
  async delList() {
    const { ctx, app } = this
    const { id } = ctx.request.body
    await app.mysql.delete('lists', { id })

    ctx.body = {
      code: 200,
      msg: '删除成功'
    }
  }
  //楼市圈添加
  async addList() {
    const { ctx, app } = this

    const { name, title, img, id, address, bianhao, date, city } = ctx.request.body

    const data = await app.mysql.insert('lists', { name, title, img, id, address, bianhao, date, city })

    ctx.body = {
      code: 200,
      msg: '请求成功',
      data
    }
  }
}

module.exports = HomeController;
